<p class="Head1"><help:paragraphinfo state="U" number="1"/><help:key-word value="names of variables" tag="kw66464_1"/><help:key-word value="variables; types and names" tag="kw66464_3"/><help:link Id="66464">Using Variables</help:link></p>
<p class="Paragraph"><help:paragraphinfo state="U" number="3"/>The following describes the basic use of variables in <help:productname>%PRODUCTNAME</help:productname> Basic.</p>
<p class="Head2"><help:paragraphinfo state="U" number="4" xmlns:help="http://openoffice.org/2000/help"/>Naming Conventions for Variable Identifiers</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="5" xmlns:help="http://openoffice.org/2000/help"/>A variable name can consist of a maximum of 255 characters. The first character of a variable name <span class="T1">must</span> be a letter A-Z or a-z. Numbers can also be used in a variable name, but punctuation symbols and special characters are not permitted, with exception of the underscore character ("_"). In <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic variable identifiers are not case-sensitive. Variable names may contain spaces but must be enclosed in square brackets if they do.</p>
<p class="P2"><help:paragraphinfo state="U" number="6" xmlns:help="http://openoffice.org/2000/help"/>Examples for variable identifiers:</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="11" xmlns:help="http://openoffice.org/2000/help"/>Not valid, variable with space must be enclosed in square brackets</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="129" xmlns:help="http://openoffice.org/2000/help"/>Not valid, special characters are not allowed</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="130" xmlns:help="http://openoffice.org/2000/help"/>Not valid, variable may not begin with a number</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="131" xmlns:help="http://openoffice.org/2000/help"/>Not valid, punctuation marks are not allowed</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="18" xmlns:help="http://openoffice.org/2000/help"/>In <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic you don't need to declare variables explicitly. An variable declaration can be performed with the <span class="T2">Dim</span> statement. You can declare more than one variable at a time by separating the names with a comma. To define the variable type, use either a type-declaration sign after the name, or the appropriate key word. </p>
<p class="P2"><help:paragraphinfo state="U" number="140" xmlns:help="http://openoffice.org/2000/help"/>Examples for variable declarations:</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="132" xmlns:help="http://openoffice.org/2000/help"/>Declares the variable "a" as a String</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="133" xmlns:help="http://openoffice.org/2000/help"/>Declares the variable "a" as a String</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="22" xmlns:help="http://openoffice.org/2000/help"/>Declares one variable as a String and one as an Integer</p>
</span></td></tr></table>
<p class="Paragraph"><help:paragraphinfo state="U" number="23" xmlns:help="http://openoffice.org/2000/help"/>It is very important when declaring variables that you use the type-declaration character each time, even if it was used in the declaration instead of a keyword. Thus the following statements are invalid:</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="26" xmlns:help="http://openoffice.org/2000/help"/>Once you have declared a variable as a certain type, you cannot declare the variable under the same name again as a different type!</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="28" xmlns:help="http://openoffice.org/2000/help"/>To force declaration of variables, use the following command:</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="30" xmlns:help="http://openoffice.org/2000/help"/>The <span class="T2">Option Explicit</span> statement has to be the first line in the module, before the first SUB. Generally, only arrays need to be declared explicitly. All other variables are declared according to the type-declaration character, or - if omitted - as the default type <span class="T2">Single</span>.</p>
<p class="P3"><help:paragraphinfo state="U" number="36" xmlns:help="http://openoffice.org/2000/help"/><span class="T2">Numeric</span> variables can contain number values. Some variables are used to store large or small numbers, and others are used for floating-point or fractional numbers. </p>
</li>
<li class="">
<p class="P3"><help:paragraphinfo state="U" number="37" xmlns:help="http://openoffice.org/2000/help"/><span class="T2">String</span> variables contain character strings.</p>
</li>
<li class="">
<p class="P3"><help:paragraphinfo state="U" number="38" xmlns:help="http://openoffice.org/2000/help"/><span class="T2">Boolean</span> variables contain either the TRUE or the FALSE value.</p>
</li>
<li class="">
<p class="P3"><help:paragraphinfo state="U" number="39" xmlns:help="http://openoffice.org/2000/help"/><span class="T2">Object</span> variables can store objects of various types, like tables and documents within a document.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="41" xmlns:help="http://openoffice.org/2000/help"/>Integer variables range from -32768 to 32767. If you assign a floating-point value to an integer variable, the decimal places are rounded to the next integer. Integer variables are rapidly calculated in procedures and are suitable for counter variables in loops. An integer variable only requires two bytes of memory. "%" is the type-declaration character.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="46" xmlns:help="http://openoffice.org/2000/help"/>Long integer variables range from -2147483648 to 2147483647. If you assign a floating-point value to a long integer variable, the decimal places are rounded to the next integer. Long integer variables are rapidly calculated in procedures and are suitable for counter variables in loops for large values. A long integer variable requires four bytes of memory. "&" is the type-declaration character.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="51" xmlns:help="http://openoffice.org/2000/help"/>Single variables can take positive or negative values ranging from 3.402823 x 10E38 to 1.401298 x 10E-45. Single variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Single variables are suitable for mathematical calculations of average precision. Calculations require more time than for Integer variables, but are faster than calculations with Double variables. A Single variable requires 4 bytes of memory. The type-declaration character is <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>"!".</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="55" xmlns:help="http://openoffice.org/2000/help"/>Double variables can take positive or negative values ranging from 1.79769313486232 x 10E308 to 4.94065645841247 x 10E-324. Double variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Double variables are suitable for precise calculations. Calculations require more time than for Single variables. A Double variable requires 8 bytes of memory. The type-declaration character is <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>"#".</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="96" xmlns:help="http://openoffice.org/2000/help"/>Currency variables are internally stored as 64-bit numbers (8 Bytes) and displayed as a fixed-decimal number with 15 non-decimal and 4 decimal places. The values range from -922337203685477.5808 to +922337203685477.5807. Currency variables are used to calculate currency values with a high precision. The <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>type-declaration character is "@".</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="59" xmlns:help="http://openoffice.org/2000/help"/>String variables can hold character strings with up to 65,535 characters. Each character is stored as the corresponding Unicode value. String variables are suitable for word processing within programs and for temporary storage of any non-printable character up to a maximum length of 64 Kbytes. The memory required for storing string variables depends on the number of characters in the variable. The type-declaration character is "$".</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="63" xmlns:help="http://openoffice.org/2000/help"/>Boolean variables store only one of two values: TRUE or FALSE. Boolean variables are used to store binary values, like the result of a comparison and are represented internally by a 2-byte integer value. Any value assigned to a Boolean is converted to "False" if the value is not exactly equal to "-1". Boolean variables can only be declared by the key words True or False.</p>
<p class="PropText"><help:paragraphinfo state="U" number="64" xmlns:help="http://openoffice.org/2000/help"/>Dim Variable As Boolean</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="66" xmlns:help="http://openoffice.org/2000/help"/>Date variables can only contain dates and time values stored in an internal format. Values assigned to Date variables with <help:link Id="66424" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Dateserial</span></help:link>, <help:link Id="66574" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Datevalue</span></help:link>, <help:link Id="66520" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Timeserial</span></help:link> or <help:link Id="66550" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Timevalue</span></help:link> are automatically converted to the internal format. Date-variables are converted to normal numbers by using the <help:link Id="66388" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Day</span></help:link>, <help:link Id="66585" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Month</span></help:link>, <help:link Id="66537" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Year</span></help:link> or the <help:link Id="66572" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Hour</span></help:link>, <help:link Id="66487" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Minute</span></help:link>, <help:link Id="66629" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Second</span></help:link> function. The internal format enables a comparison of date/time values by calculating the difference between two numbers. These variables can only be declared with the key word <span class="T2">Date</span>.</p>
<p class="PropText"><help:paragraphinfo state="U" number="67" xmlns:help="http://openoffice.org/2000/help"/>Dim Variable As Date</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="69" xmlns:help="http://openoffice.org/2000/help"/>As soon as the variable has been declared, it is automatically set to the "Null" value. Note the following conventions:</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="70" xmlns:help="http://openoffice.org/2000/help"/><span class="T2">Numeric</span> variables are automatically assigned the value "0" as soon as they are declared.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="71" xmlns:help="http://openoffice.org/2000/help"/><span class="T2">Date variables</span> are assigned the value 0 internally; equivalent to converting the value to "0" with the <help:link Id="66388" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Day</span></help:link>, <help:link Id="66585" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Month</span></help:link>, <help:link Id="66537" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Year</span></help:link> or the <help:link Id="66572" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Hour</span></help:link>, <help:link Id="66487" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Minute</span></help:link>, <help:link Id="66629" xmlns:help="http://openoffice.org/2000/help"><span class="T2">Second</span></help:link> function.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="72" xmlns:help="http://openoffice.org/2000/help"/><span class="T2">String variables</span> are assigned an empty-string ("") when they are declared.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="84" xmlns:help="http://openoffice.org/2000/help"/><help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic knows one- or multi-dimensional arrays, defined by a specified variable type. Arrays are suitable for editing lists and tables in programs. Individual elements of an array can be addressed through a numeric index.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="85" xmlns:help="http://openoffice.org/2000/help"/>Arrays <span class="T1">must</span> be declared with the <span class="T2">Dim</span> statement. There are several ways to define the index range of an array:</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="137" xmlns:help="http://openoffice.org/2000/help"/>30 elements (a matrix of 6 x 5 elements)</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="89" xmlns:help="http://openoffice.org/2000/help"/>21 elements (including 0), numbered from -15 to 5</p>
</span></td></tr></table>
<p class="Paragraph"><help:paragraphinfo state="U" number="90" xmlns:help="http://openoffice.org/2000/help"/>The index range can include positive as well as negative numbers. The maximum number of elements that can be addressed through an index is 16368.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="92" xmlns:help="http://openoffice.org/2000/help"/>Constants have a fixed value. They are only defined once in the program and cannot be redefined later:</p>